<?
/*
 *      router.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class Router{

	private $routedefaults;
	public $route;
	public $arguments=array();
	protected $db;

	function Router($request)
	{
		global $db;
		$this->db = $db;
		$proutes = $this->parseURI($request->uri);
		$routes = $this->loadRoutes();
		$froutes = $this->filterRoutes($routes);
		$this->route = $this->findRoute($froutes, $proutes, $request->method);
		$this->arguments = $this->findArguments($request->uri, $this->route);
		if((count($this->arguments) > $this->route['maxa']) OR (count($this->arguments) < $this->route['mina']))
			throw new Service_Exception('Requested page not found');
	}

	//Preformat possible routes from uri
	protected function parseURI($current_uri)
	{
		$parts = split('/',$current_uri);
		$proutes[0]='';
		for($i = 0; $i<count($parts); $i++)
		$proutes[$i+1] = trim($proutes[$i].'/'.$parts[$i],'/');
		$proutes=array_reverse($proutes);
		return $proutes;
	}

	//Delete erroneous routes
	protected function filterRoutes($routes)
	{
		foreach($routes as $name=>$route)
		{
			if(!isset($route['uri']))
				{
					//Wrong route. Remove it.
					unset($routes[$name]);
					continue;
				}
		}
		return $routes;
	}

	function findRoute($routes, $proutes, $request_method)
	{
		foreach ($proutes as $proute)
		{
			foreach ($routes as $name => $route)
			{
				if(isset($route['method']) AND $route['method'] !== $request_method)
				{
					//Request method not matched
					unset($routes[$name]);
					continue;
				}
				
				if($proute != $route['uri']){
					//Check not passed
					continue;
				}
				$found = $name;
				break;
			}
			if(isset($found)){
				return $routes[$found];
			}
		}
	}

	protected function findArguments($currentURI, $route)
	{
		$args = trim(substr($currentURI,strlen($route['uri'])),'/');
		$r = explode('/', $args);
		if (implode('',$r) == '') $r = array();
		return $r;
	}

	protected function loadRoutes()
	{
		$routes = array(
			'idle' => array(
					'uri' => '',
					'leadsto' => 'CIdle',
					'method' => 'get',
					'maxa' => 0,
					'mina' => 0
				),
			'base' => array(
					'uri' => '',
					'leadsto' => 'CIdle',
					'method' => 'post',
					'maxa' => 0,
					'mina' => 0
				),
			'register' => array(
					'uri' => 'register',
					'leadsto' => 'CRegister',
					'method' => 'get',
					'maxa' => 0,
					'mina' => 0
				),
			'register-do' => array(
					'uri' => 'register',
					'leadsto' => 'CRegister',
					'method' => 'post',
					'maxa' => 0,
					'mina' => 0
				),
			'inventory' => array(
					'uri' => 'inv',
					'leadsto' => 'CInv',
					'method' => 'get',
					'maxa' => 0,
					'mina' => 0
				),
			'inventory-do' => array(
					'uri' => 'inv',
					'leadsto' => 'CInv',
					'method' => 'post',
					'maxa' => 0,
					'mina' => 0
				)
		);
		
		$this->routedefaults = array
		(
			'active' => 1,
		);
		
		return $routes;
	}
	
}
